const app = getApp()
// shouY/fabu_XQ/fabu_XQ.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message: {
      title: '',
      content: '',
      date: '',
      dizhi: '',
      url: '',
      type: '',
      tele: '',
      ipt: '',
    },
    location: {
      longitude: '',
      latitude: ''
    },
    fileID: "",
    cloudPath: "",
    imagePath: "",
  },
  addimg() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {

        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]

        // 上传图片
        const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            this.fuzhi()
            console.log(app.globalData)
            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            let {
              fileID,
              cloudPath,
              imagePath,
            } = app.globalData

            this.setData({
              fileID,
              cloudPath,
              imagePath,
            })
            console.log('[上传文件] 成功：', res, this.data.imagePath)
            console.log(this.data.imagePath)
            // wx.navigateTo({
            //   url: '/pages/storageConsole/storageConsole'
            // })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },

  fuzhi() {
    app.globalData.fileID = res.fileID
    app.globalData.cloudPath = cloudPath
    app.globalData.imagePath = filePath
    let {
      fileID,
      cloudPath,
      imagePath,
    } = app.globalData

    this.setData({
      fileID,
      cloudPath,
      imagePath,
    })
    console.log(this.data.imagePath)
  },



  formSubmit: function(e) {
    // 时间戳转时间
    var time = require('../../my/index/data.js');
    var myDate = new Date();
    var sjc = myDate.getTime()
    var time = time.formatTime(sjc, 'Y/M/D h:m:s')
    let {
      content,
      dizhi,
      tele,
    } = e.detail.value
    wx.getStorage({
      key: 'dxd_type',
      success: (res) => {
        this.setData({
          message: {
            content: content,
            tele: tele,
            dizhi: dizhi,
            type: res.data
          }
        })
        wx.getStorage({
          key: 'my',
          success: (data) => {
            this.setData({
              message: {
                title: data.data.title,
                url: data.data.url,
                content: this.data.message.content,
                tele: this.data.message.tele,
                type: this.data.message.type,
                dizhi: this.data.message.dizhi,
                date: time,
                ipt: '',
              }
            })
            if (this.data.message.content == '') {
              wx.showToast({
                title: '请输入内容',
                icon: 'none',
                duration: 1500
              })
              return false
            }
            const db = wx.cloud.database()
            db.collection('dxd_get').add({
              data: this.data.message
            }).then(res => {
              this.setData({
                message: {
                  title: '',
                  content: '',
                  data: '',
                  dizhi: '',
                  url: '',
                  type: '',
                  tele: '',
                  ipt:''
                }
              })
              wx.switchTab({
                url: '/my/index/index',
              })
            }).catch(success => {})
          },
        })


      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取地理位置，存本地，渲染页面
    wx.getLocation({
      success: (res) => {
        //保存到data里面的location里面
        this.setData({
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        })
        var QQMapWX = require('../../shouY/diLiDW/qqmap-wx-jssdk.js');

        var demo = new QQMapWX({
          key: 'SQOBZ-74A6I-RGVGK-5IT7D-4E227-7TB4L' // 必填
        });
        demo.reverseGeocoder({
          location: {
            latitude: this.data.location.latitude,
            longitude: this.data.location.longitude
          },
          success: (res) => {
            wx.setStorage({
              key: 'dizhi',
              data: res.result.address,
            })
            this.setData({
              message: {
                dizhi: res.result.address
              }
            })
          },
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})